<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { showConfirmDialog, showToast } from 'vant';
import { useAuthStore } from '@/stores/auth';
import tarbar from '@/layout/tarbar/tarbar.vue';
import navbar from '@/layout/header/navbar.vue';

const router = useRouter();
const authStore = useAuthStore();
const username = ref(authStore.username);

// 版本信息
const version = ref('1.0.0');

const handleLogout = async () => {
  try {
    await showConfirmDialog({
      title: '确认登出',
      message: '您确定要退出登录吗？',
    }).then(() => {
      authStore.logout();
      router.push({ name: 'Login' });
      showToast('已成功退出');
    }).catch(() => {
      console.log('用户取消了登出');
    });
  } catch (error) {
    console.error('登出失败:', error);
  }
};
</script>

<template>
  <navbar />
  <div class="setting-container">
    <!-- 用户信息卡片 -->
    <div class="user-card">
      <div class="avatar">
        <van-icon name="manager" size="48" />
      </div>
      <div class="user-info">
        <h3 class="username">{{ username }}</h3>
        <span class="role">管理员</span>
      </div>
    </div>

    <!-- 设置选项组 -->
    <div class="settings-section">
      <h4 class="section-title">账户设置</h4>
      <van-cell-group inset class="setting-group">
        <van-cell 
          title="个人信息" 
          is-link
          icon="user-o"
        />
        <van-cell 
          title="修改密码" 
          is-link
          icon="lock"
        />
        <van-cell 
          title="消息通知" 
          is-link
          icon="bell"
        />
      </van-cell-group>

      <h4 class="section-title">系统</h4>
      <van-cell-group inset class="setting-group">
        <van-cell 
          title="版本信息" 
          :value="version"
          icon="info-o"
        />
        <van-cell 
          title="检查更新" 
          is-link
          icon="upgrade"
        />
        <van-cell 
          title="帮助中心" 
          is-link
          icon="question-o"
        />
      </van-cell-group>

      <!-- 退出登录按钮 -->
      <div class="logout-section">
        <van-button 
          round 
          block 
          type="danger" 
          class="logout-button"
          @click="handleLogout"
        >
          退出登录
        </van-button>
      </div>
    </div>
  </div>
  <tarbar />
</template>

<style scoped>
.setting-container {
  padding: 16px;
  background-color: #f5f5f5;
  min-height: calc(100vh - 100px);
}

.user-card {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.avatar {
  width: 64px;
  height: 64px;
  background: #f0f2f5;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
}

.user-info {
  flex: 1;
}

.username {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;
  color: #333;
}

.role {
  font-size: 14px;
  color: #666;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #666;
  margin: 24px 0 12px 12px;
}

.setting-group {
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 16px;
}

.logout-section {
  margin-top: 32px;
  padding: 0 16px;
}

.logout-button {
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}

:deep(.van-cell) {
  align-items: center;
  padding: 16px;
}

:deep(.van-cell__title) {
  font-size: 15px;
}

:deep(.van-icon) {
  font-size: 20px;
  color: #666;
}

:deep(.van-cell-group--inset) {
  margin: 0;
}
</style>